package com.sjzs.web.mail;

import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;



/**
 * 
 * @description: 邮件发送
 */
public class GmailSender {
	private static final Logger LOGGER = Logger.getLogger(GmailSender.class);

	/**
	 * 
	 * @param protocol 邮箱会话的协议名称
	 * @return Session
	 * @description: 创建会话
	 */
	public Session createSession(String protocol) {
		Properties property = new Properties();
		property.setProperty("mail.transport.protocol", protocol);
		property.setProperty("mail.smtp.auth", "true");

		Session session = Session.getInstance(property);

		// 启动JavaMail调试功能，可以返回与SMTP服务器交互的命令信息
		// 可以从控制台中看一下服务器的响应信息
		session.setDebug(true);
		return session;
	}

	/**
	 * 
	 * @param mail 邮件发件箱内容
	 * @param session 会话
	 * @param msg 邮件的信息MimeMessage对象
	 * @description: 发送单个邮件
	 */
	public void sendMail(MailServer mail, Session session, MimeMessage msg) {
		// 由 Session 对象获得 Transport 对象
		Transport transport = null;
		try {
			transport = session.getTransport();
			// 发送用户名、密码连接到指定的 smtp 服务器
			transport.connect(mail.getSmtpServer(), mail.getUsername(), mail.getPassword());

			transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
		} catch (Exception e) {
			LOGGER.error(e);
		}

		try {
			transport.close();
		} catch (MessagingException e) {
			LOGGER.error(e);
		}
	}

	/**
	 * 
	 * @param mail 发件箱信息
	 * @param session 会话
	 * @param msgs 邮件的信息MimeMessage对象
	 * @return int
	 * @throws Exception 异常
	 * @description: 批量发送邮件信息
	 */
	public int sendMail(MailServer mail, Session session, List<MimeMessage> msgs) throws Exception {
		// 由 Session 对象获得 Transport 对象
		Transport transport = session.getTransport();
		// 发送用户名、密码连接到指定的 smtp 服务器
		transport.connect(mail.getSmtpServer(), mail.getUsername(), mail.getPassword());

		int successSize = 0;
		for (MimeMessage msg : msgs)
			try {
				transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
				successSize++;
			} catch (Exception e) {
				if (e.getMessage().equals("Invalid Addresses")) // 错误原因：收件人邮箱不存在;
					LOGGER.info("收件人邮箱地址不存在");
				LOGGER.error(e);
			}
		transport.close();
		return successSize;
	}

	/**
	 * 
	 * @param transport 协议名称
	 * @param msg 邮件的信息MimeMessage对象
	 * @return boolean
	 * @description: 发送邮件
	 */
	public boolean sendMail(Transport transport, MimeMessage msg) {
		try {
			transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
		} catch (MessagingException e) {
			LOGGER.error(e);
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param mail 发件箱信息
	 * @param transport 协议
	 * @return boolean
	 * @description: 连接发件箱，验证是否正确
	 */
	public boolean connectMailServer(MailServer mail, Transport transport) {
		try {
			transport.connect(mail.getSmtpServer(), mail.getUsername(), mail.getPassword());
		} catch (MessagingException e) {
			LOGGER.error(e);
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @param session
	 * @param msg
	 * @throws Exception
	 * @description:
	 */
	public void sendMail(Session session, MimeMessage msg) throws Exception {
		MailServer server = new MailServer();
		server.setSmtpServer(MailCofing.SMTP);
		server.setUsername(MailCofing.YXDZ);
		server.setPassword(MailCofing.YXMM);

		// 由 Session 对象获得 Transport 对象
		Transport transport = null;
		transport = session.getTransport();
		// 发送用户名、密码连接到指定的 smtp 服务器
		transport.connect(server.getSmtpServer(), server.getUsername(), server.getPassword());
		transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
		transport.close();
	}
}
